package com.zhms.cape;
import org.codehaus.groovy.ast.ASTNode
import org.codehaus.groovy.ast.ConstructorNode
import org.codehaus.groovy.ast.GroovyClassVisitor
import org.codehaus.groovy.control.CompilePhase
import org.codehaus.groovy.control.SourceUnit
import org.codehaus.groovy.syntax.SyntaxException
import org.codehaus.groovy.transform.ASTTransformation
import org.codehaus.groovy.transform.GroovyASTTransformation

@GroovyASTTransformation(phase = CompilePhase.SEMANTIC_ANALYSIS)
class CodeCheck implements ASTTransformation{
    void visit(ASTNode[] astNodes, SourceUnit sourceUnit){
        sourceUnit.ast.classes.each {
            classNode ->
                classNode.visitContents(new OurClassVisitor(sourceUnit))
        }
    }
}

class OurClassVisitor implements GroovyClassVisitor{
    SourceUnit sourceUnit
    OurClassVisitor(theSourceUnit){
        sourceUnit = theSourceUnit
    }

    private void reportError(message, lineNumber, columnNumber){
        sourceUnit.addError(new SyntaxException(message, lineNumber, columnNumber))
    }

    void visitMethod(org.codehaus.groovy.ast.MethodNode node){
        if (node.name.size() == 1){
            reportError "Make method name descriptive, avoid single letter names",
                    node.lineNumber, node.columnNumber

            node.parameters.each {
                parameter ->
                    if (parameter.name.size() == 1){
                        reportError "Single letter parameters are morally wrong!",
                                parameter.lineNumber, parameter.columnNumber
                    }
            }
        }
    }

    @Override
    void visitClass(org.codehaus.groovy.ast.ClassNode node) {

    }

    @Override
    void visitField(org.codehaus.groovy.ast.FieldNode node) {

    }

    @Override
    void visitConstructor(ConstructorNode node) {

    }

    @Override
    void visitProperty(org.codehaus.groovy.ast.PropertyNode node) {

    }


}





